import time

import requests
import json
import out_head
import version_list_head
import in_version_head

file = open("data/接口统计表2.txt", "w", encoding="utf-8")
for i in range(51, 52):
    response = requests.get('https://admin-dshare.wenzhou.gov.cn/website/v2/interface/?page={}'.format(i),
                            headers=out_head.headers,
                            params=out_head.params,
                            cookies=out_head.cookies)
    json_context = json.loads(response.text)
    print("正在爬取第{}页数据".format(i))
    for j in range(0, 20):
        # 获得id,interface_name,alias_name
        id = json_context["data"][j]["id"]
        interface_name = json_context["data"][j]["interface_name"]
        alias_name = json_context["data"][j]["alias_name"]
        # 从外面的页面获得数组长度，也就是版本的多少。
        req = requests.get(url=version_list_head.url.format(int(id)), headers=version_list_head.headers,
                           cookies=version_list_head.cookies)
        # 判断版本号逻辑:如果最后一个版本的状态为DONE就说明这个版本号是对的，反之版本号-1，用version接收这个版本号
        if json.loads(req.text)["data"][len(json.loads(req.text)["data"]) - 1]["sts"] == "DONE":
            version = json.loads(req.text)["data"][len(json.loads(req.text)["data"]) - 1]["version"]
        else:
            version = json.loads(req.text)["data"][len(json.loads(req.text)["data"]) - 2]["version"]
        req2 = requests.get(url=in_version_head.url.format(int(id), int(version)), headers=in_version_head.headers,
                            cookies=in_version_head.cookies)
        # 将每个字段变成一个列表
        split = str(json.loads(req2.text)["data"]["interface_property"]["sql"]).split("from")[0].replace(
            "select ", "").split(",")
        table = str(json.loads(req2.text)["data"]["interface_property"]["table_name"])
        # 写入文件
        file.writelines(str(id) + "," + interface_name + "," + alias_name + "," + table + "," + str(split) + "\n")
        time.sleep(0.5)
file.close()
